<!DOCTYPE html>
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <meta name="description" content="tong.li&#39;s blog">
  <meta name="keyword" content="彤哥哥博客，95后技术爱好者,现就职于同程旅行/同程艺龙上海分公司，专注于互联网技术分享的平台。">
  
    <link rel="shortcut icon" href="/css/images/icon.png">
  
  <title>
    
      Linux基础-正则表达式之三剑客 | 彤哥哥的博客
    
  </title>
  <link href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.css" rel="stylesheet">
  <link href="https://cdn.staticfile.org/highlight.js/9.12.0/styles/tomorrow-night.min.css" rel="stylesheet">
  
<link rel="stylesheet" href="/css/style.css">

  
  <script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/geopattern/1.2.3/js/geopattern.min.js"></script>
  <script src="https://cdn.staticfile.org/nprogress/0.2.0/nprogress.min.js"></script>
  
    
<script src="/js/qrious.js"></script>

  
  
  
  
    <!-- MathJax support START -->
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="https://cdn.staticfile.org/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <!-- MathJax support END -->
  


  
  
    
<script src="/js/local-search.js"></script>


<meta name="generator" content="Hexo 5.4.2"></head>
<div class="wechat-share">
  <img src="/css/images/logo.png" />
</div>
  <body>
    <header class="header fixed-header">
  <div class="header-container">
    <a class="home-link" href="/">
      <div class="logo"></div>
      <span>彤哥哥的博客</span>
    </a>
    <ul class="right-list">
      
        <li class="list-item">
          
            <a href="/" class="item-link">主页</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/series/" class="item-link">分类</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/tags/" class="item-link">标签</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/archives/" class="item-link">归档</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/project/" class="item-link">项目</a>
          
        </li>
      
        <li class="list-item">
          
            <a href="/about/" class="item-link">关于</a>
          
        </li>
      
      
        <li class="menu-item menu-item-search right-list">
    <a role="button" class="popup-trigger">
        <i class="fa fa-search fa-fw"></i>
    </a>
</li>
      
    </ul>
    <div class="menu">
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
      <span class="icon-bar"></span>
    </div>
    <div class="menu-mask">
      <ul class="menu-list">
        
          <li class="menu-item">
            
              <a href="/" class="menu-link">主页</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/series/" class="menu-link">分类</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/tags/" class="menu-link">标签</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/archives/" class="menu-link">归档</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/project/" class="menu-link">项目</a>
            
          </li>
        
          <li class="menu-item">
            
              <a href="/about/" class="menu-link">关于</a>
            
          </li>
        
      </ul>
    </div>
    
      <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
            <span class="search-icon">
                <i class="fa fa-search"></i>
            </span>
            <div class="search-input-container">
                <input autocomplete="off" autocapitalize="off"
                    placeholder="Please enter your keyword(s) to search." spellcheck="false"
                    type="search" class="search-input">
            </div>
            <span class="popup-btn-close">
                <i class="fa fa-times-circle"></i>
            </span>
        </div>
        <div id="search-result">
            <div id="no-result">
                <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
            </div>
        </div>
    </div>
</div>
    
  </div>
</header>

    <div id="article-banner">
  <h2>Linux基础-正则表达式之三剑客</h2>
  <p class="post-date">2018-05-22</p>
  <div class="arrow-down">
    <a href="javascript:;"></a>
  </div>
</div>
<main class="app-body flex-box">
  <!-- Article START -->
  <article class="post-article">
    <section class="markdown-content"><p>Linux的世界里一切皆文件,对Linux的操作就是对文件的处理.被业界称为”三剑客”的awk,sed,grep就能够帮助我们更方便更高效的处理文件.<br>“三剑客”中grep擅长查找功能,sed擅长取行和替换,awk擅长取列.<br>“三剑客”就是普通的Linux命令,而正则表达式好比一个模板,两者结合使用,能够发挥巨大的作用,尤其是在Shell脚本中,使我们的代码短小精悍.总之就是一句话:提高工作效率. </p>
<h2 id="正则表达式"><a href="#正则表达式" class="headerlink" title="正则表达式"></a>正则表达式</h2><p>字符串:”abcaxc”,正则表达式为:ab.*c</p>
<ul>
<li>贪婪模式: 编程中默认模式,正则表达式一般趋向于最大长度[匹配.如正则ab.*c最大匹配为:abcaxc</li>
<li>非贪婪模式: 正则匹配到结果就好,说白了就是最少匹配.如正则ab.*c最大匹配为:abc  <h2 id="grep命令"><a href="#grep命令" class="headerlink" title="grep命令"></a>grep命令</h2>grep命令是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行.<br>unix的grep家族包括grep,egrep和fgrep,笔者在这不一一列举了,读者可自行研究.<h3 id="常用选项"><a href="#常用选项" class="headerlink" title="常用选项"></a>常用选项</h3>　　-c : 计算找到’搜寻字符串’的次数<br>　　-i : 忽略大小写进行搜索<br>　　-n : 顺便输出行号<br>　　-v : 反向选择,取反搜索<br>　　-color=auto : 可以找到的搜索关键字加上颜色显示</li>
</ul>
<h3 id="实战示例"><a href="#实战示例" class="headerlink" title="实战示例"></a>实战示例</h3><p>1.通过管道查找配置文件里的字符</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# cat server.conf | grep port</span><br><span class="line">port : 9001</span><br></pre></td></tr></table></figure>
<p>2.通过管道查找以*.conf结尾的文件  </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# ll | grep *.conf</span><br><span class="line">-rw-r--r-- 1 root root    22 May 18 14:51 server.conf</span><br></pre></td></tr></table></figure>
<p>3.根据关键字查找进程信息</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# ps -ef | grep pptpd</span><br><span class="line">root     29510     1  0 14:55 ?        00:00:00 /usr/sbin/pptpd -f</span><br><span class="line">root     30061 25475  0 14:58 pts/2    00:00:00 grep --color=auto pptpd</span><br></pre></td></tr></table></figure>
<h2 id="sed命令"><a href="#sed命令" class="headerlink" title="sed命令"></a>sed命令</h2><p>sed是一项Linux指令,功能同awk类似,差别在于,sed简单,擅长取行和替换,对列处理的功能要差一些,awk的功能复杂,对列处理的功能比较强大.  </p>
<h3 id="命令格式"><a href="#命令格式" class="headerlink" title="命令格式"></a>命令格式</h3><ul>
<li>sed [options] ‘command’ file(s)</li>
<li>sed [options] -f scriptfile file(s)<h3 id="取行选项"><a href="#取行选项" class="headerlink" title="取行选项"></a>取行选项</h3></li>
<li>a\ 在当前行下面插入文本.     </li>
<li>i\ 在当前行上面插入文本.    </li>
<li>c\ 把选定的行改为新的文本.    </li>
<li>d 删除,删除选择的行.    </li>
<li>D 删除模板块的第一行.    </li>
<li>s 替换指定字符</li>
<li>h 拷贝模板块的内容到内存中的缓冲区.    </li>
<li>H 追加模板块的内容到内存中的缓冲区.    </li>
<li>g 获得内存缓冲区的内容,并替代当前模板块中的文本.    </li>
<li>G 获得内存缓冲区的内容,并追加到当前模板块文本的后面.    </li>
<li>l 列表不能打印字符的清单.    </li>
<li>n 读取下一个输入行,用下一个命令处理新的行而不是用第一个命令.    </li>
<li>N 追加下一个输入行到模板块后面并在二者间嵌入一个新行,改变当前行号码.    </li>
<li>p 打印模板块的行.    </li>
<li>P(大写) 打印模板块的第一行.    </li>
<li>q 退出Sed.    </li>
<li>b lable 分支到脚本中带有标记的地方,如果分支不存在则分支到脚本的末尾.    </li>
<li>r file 从file中读行.    </li>
<li>t label if分支,从最后一行开始,条件一旦满足或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾.    </li>
<li>T label 错误分支,从最后一行开始,一旦发生错误或者T,t命令,将导致分支到带有标号的命令处,或者到脚本的末尾.    </li>
<li>w file 写并追加模板块到file末尾.      </li>
<li>W file 写并追加模板块的第一行到file末尾.      </li>
<li>! 表示后面的命令对所有没有被选定的行发生作用.      </li>
<li>= 打印当前行号码.      </li>
<li># 把注释扩展到下一个换行符以前.      <h3 id="替换选项"><a href="#替换选项" class="headerlink" title="替换选项"></a>替换选项</h3></li>
<li>g 表示行内全面替换.  </li>
<li>p 表示打印行.  </li>
<li>w 表示把行写入一个文件.  </li>
<li>x 表示互换模板块中的文本和缓冲区中的文本.  </li>
<li>y 表示把一个字符翻译为另外的字符（但是不用于正则表达式）</li>
<li>\1 子串匹配标记</li>
<li>&amp; 已匹配字符串标记<h3 id="实战示例-1"><a href="#实战示例-1" class="headerlink" title="实战示例"></a>实战示例</h3></li>
</ul>
<ol>
<li>获取文件的行内容<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# sed -n 2p /etc/passwd  # 获取/ect/passwd第二行</span><br><span class="line">bin:x:1:1:bin:/bin:/sbin/nologin</span><br><span class="line">[root@litong ~]# sed -n 2,7p /etc/passwd  # 获取/ect/passwd第二行到第七行</span><br><span class="line">bin:x:1:1:bin:/bin:/sbin/nologin</span><br><span class="line">daemon:x:2:2:daemon:/sbin:/sbin/nologin</span><br><span class="line">adm:x:3:4:adm:/var/adm:/sbin/nologin</span><br><span class="line">lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin</span><br><span class="line">sync:x:5:0:sync:/sbin:/bin/sync</span><br><span class="line">shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown  </span><br></pre></td></tr></table></figure></li>
<li>替换文本<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# sed -i &#x27;s/port/pp/g&#x27; server.conf  # 文件中含有port为全局替换pp,,并不会真正进行文件替换,而只是把操作后的文本返回</span><br><span class="line">[root@litong ~]# cat server.conf </span><br><span class="line">server</span><br><span class="line">server</span><br><span class="line">   pp : 9001</span><br><span class="line">   pp : 9001</span><br></pre></td></tr></table></figure></li>
<li>删除文本<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# sed &#x27;2d&#x27; server.conf  # 删除文件的第二行,并不会真正删除文件行,而只是把操作后的文本返回</span><br><span class="line">server</span><br><span class="line">   pp : 9001</span><br><span class="line">   pp : 9001</span><br><span class="line">[root@litong ~]# sed &#x27;1,$d&#x27; server.conf #文件清空,,并不会真正删除文件行,而只是把操作后的文本返回</span><br></pre></td></tr></table></figure>
<h2 id="awk命令"><a href="#awk命令" class="headerlink" title="awk命令"></a>awk命令</h2>awk是一个优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一.这个命令擅长取列.<br>awk经过改进生成新的版本nawk和gawk,现在默认linux系统下日常使用gawk.   <h3 id="命令格式-1"><a href="#命令格式-1" class="headerlink" title="命令格式"></a>命令格式</h3></li>
</ol>
<ul>
<li>awk [选项参数] ‘script’ var=value file(s)</li>
<li>awk [选项参数] -f scriptfile var=value file(s)  <h3 id="选项参数"><a href="#选项参数" class="headerlink" title="选项参数"></a>选项参数</h3></li>
<li>-F fs or –field-separator fs :指定输入文件折分隔符,fs是一个字符串或者是一个正则表达式,如-F:.</li>
<li>-v var=value or –asign var=value<br>赋值一个用户定义变量.</li>
<li>-f scripfile or –file scriptfile,从脚本文件中读取awk命令.</li>
<li>-mf nnn and -mr nnn,对nnn值设置内在限制,-mf选项限制分配给nnn的最大块数目;-mr选项限制记录的最大数目.这两个功能是Bell实验室版awk的扩展功能,在标准awk中不适用.</li>
<li>-W compact or –compat, -W traditional or –traditional,在兼容模式下运行awk.所以gawk的行为和标准的awk完全一样,所有的awk扩展都被忽略.</li>
<li>-W copyleft or –copyleft, -W copyright or –copyright,打印简短的版权信息.</li>
<li>-W help or –help, -W usage or –usage,打印全部awk选项和每个选项的简短说明.</li>
<li>-W lint or –lint,打印不能向传统unix平台移植的结构的警告.</li>
<li>-W lint-old or –lint-old,打印关于不能向传统unix平台移植的结构的警告.</li>
<li>-W posix,打开兼容模式.但有以下限制,不识别：/x,函数关键字,func,换码序列以及当fs是一个空格时,将新行作为一个域分隔符;操作符<strong>和</strong>=不能代替^和^=;fflush无效.</li>
<li>-W re-interval or –re-inerval,允许间隔正则表达式的使用,参考(grep中的Posix字符类),如括号表达式[[:alpha:]].</li>
<li>-W source program-text or –source program-text,使用program-text作为源代码,可与-f命令混用.</li>
<li>-W version or –version,打印bug报告信息的版本.  <h3 id="实战示例-2"><a href="#实战示例-2" class="headerlink" title="实战示例"></a>实战示例</h3></li>
</ul>
<p>1.按照指定的分隔符取列 </p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# cut -d: -f 1 /etc/passwd  # cut命令也可以取列</span><br><span class="line">root</span><br><span class="line">bin</span><br><span class="line">daemon</span><br><span class="line">adm</span><br><span class="line">lp</span><br><span class="line">sync</span><br><span class="line">shutdown</span><br><span class="line">halt</span><br><span class="line">mail</span><br><span class="line">[root@litong ~]# awk -F: &#x27;&#123;print $1&#125;&#x27; /etc/passwd # awk命令取列</span><br><span class="line">root</span><br><span class="line">bin</span><br><span class="line">daemon</span><br><span class="line">adm</span><br><span class="line">lp</span><br><span class="line">sync</span><br><span class="line">shutdown</span><br><span class="line">halt</span><br><span class="line">mail</span><br></pre></td></tr></table></figure>
<h2 id="企业实战-用三剑客取IP"><a href="#企业实战-用三剑客取IP" class="headerlink" title="企业实战-用三剑客取IP"></a>企业实战-用三剑客取IP</h2><figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">[root@litong ~]# ifconfig eth0 #查看eth0这块网卡的ip</span><br><span class="line">eth0: flags=<span class="number">4163</span>&amp;<span class="keyword">lt</span>;UP,BROADCAST,RUNNING,MULTICAST&gt;  mtu <span class="number">1500</span></span><br><span class="line">        inet <span class="number">172.21</span>.<span class="number">205.254</span>  netmask <span class="number">255.255</span>.<span class="number">240.0</span>  broadcast <span class="number">172.21</span>.<span class="number">207.255</span></span><br><span class="line">        ether <span class="number">00</span>:<span class="number">16</span>:<span class="number">3</span><span class="keyword">e</span>:<span class="number">00</span>:a1:e1  txqueuelen <span class="number">1000</span>  (Ethernet)</span><br><span class="line">        RX packets <span class="number">599643</span>  bytes <span class="number">144572651</span> (<span class="number">137.8</span> MiB)</span><br><span class="line">        RX errors <span class="number">0</span>  dropped <span class="number">0</span>  overruns <span class="number">0</span>  frame <span class="number">0</span></span><br><span class="line">        TX packets <span class="number">590688</span>  bytes <span class="number">161445142</span> (<span class="number">153.9</span> MiB)</span><br><span class="line">        TX errors <span class="number">0</span>  dropped <span class="number">0</span> overruns <span class="number">0</span>  carrier <span class="number">0</span>  collisions <span class="number">0</span></span><br><span class="line">[root@litong ~]# ifconfig eth0 | <span class="keyword">grep</span> -Po <span class="string">&#x27;(?&amp;lt;=inet )\S+&#x27;</span>  # <span class="keyword">grep</span>取IP方式一</span><br><span class="line"><span class="number">172.21</span>.<span class="number">205.254</span></span><br><span class="line">[root@litong ~]# ifconfig eth0 | <span class="keyword">grep</span> -<span class="keyword">o</span> <span class="string">&quot;inet [0-9.]*&quot;</span> | <span class="keyword">grep</span> -<span class="keyword">o</span> <span class="string">&quot;[0-9.]*&quot;</span>  # <span class="keyword">grep</span>取IP方式二</span><br><span class="line"><span class="number">172.21</span>.<span class="number">205.254</span></span><br><span class="line">[root@litong ~]# ifconfig eth0 | awk -F <span class="string">&quot;[ ]+&quot;</span> <span class="string">&#x27;NR==2 &#123;print $3&#125;&#x27;</span>  # awk取IP</span><br><span class="line"><span class="number">172.21</span>.<span class="number">205.254</span></span><br><span class="line">[root@litong ~]# ifconfig eth0 | sed -nr <span class="string">&#x27;2s#^[^0-9]*([0-9.]*).*$#\1#gp&#x27;</span>  # sed取IP方式一</span><br><span class="line"><span class="number">172.21</span>.<span class="number">205.254</span></span><br><span class="line">[root@litong ~]# ifconfig eth0 | sed -nr <span class="string">&#x27;2s#^.*inet (.*) net.*$#\1#gp&#x27;</span> # sed取IP方式二</span><br><span class="line"><span class="number">172.21</span>.<span class="number">205.254</span> </span><br><span class="line">[root@litong ~]# ifconfig eth0 | sed -n <span class="string">&#x27;2p&#x27;</span> | sed <span class="string">&#x27;s#^.*inet ##g&#x27;</span> | sed <span class="string">&#x27;s# netmask.*$##g&#x27;</span> # sed取IP方式三</span><br><span class="line"><span class="number">172.21</span>.<span class="number">205.254</span></span><br></pre></td></tr></table></figure>
<h2 id="相关文档"><a href="#相关文档" class="headerlink" title="相关文档"></a>相关文档</h2><p><a target="_blank" rel="noopener" href="http://www.runoob.com/linux/linux-comm-awk.html">菜鸟教程</a><br><a target="_blank" rel="noopener" href="https://www.cnblogs.com/sinsenliu/p/9037386.html">三剑客取IP</a></p>
</section>
    <!-- Tags START -->
    
      <div class="tags">
        <span>Tags:</span>
        
  <a href="/tags#Linux" >
    <span class="tag-code">Linux</span>
  </a>

      </div>
    
    <!-- Tags END -->
    <!-- NAV START -->
    
  <div class="nav-container">
    <!-- reverse left and right to put prev and next in a more logic postition -->
    
      <a class="nav-left" href="/2018/05/22/Linux%E5%9F%BA%E7%A1%80-wget%E4%B8%8Ecurl%E5%91%BD%E4%BB%A4/">
        <span class="nav-arrow">← </span>
        
          Linux基础-wget与curl命令
        
      </a>
    
    
      <a class="nav-right" href="/2018/05/24/Spring%20Cloud-Zuul%E6%A0%B8%E5%BF%83%E8%BF%87%E6%BB%A4%E5%99%A8%E5%8F%8A%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86/">
        
          Spring Cloud-Zuul核心过滤器及异常处理
        
        <span class="nav-arrow"> →</span>
      </a>
    
  </div>

    <!-- NAV END -->
    <!-- 打赏 START -->
    
      <div class="money-like">
        <div class="reward-btn">
          赏
          <span class="money-code">
            <span class="alipay-code">
              <div class="code-image"></div>
              <b>使用支付宝打赏</b>
            </span>
            <span class="wechat-code">
              <div class="code-image"></div>
              <b>使用微信打赏</b>
            </span>
          </span>
        </div>
        <p class="notice">若你觉得我的文章对你有帮助，欢迎点击上方按钮对我打赏</p>
      </div>
    
    <!-- 打赏 END -->
    <!-- 二维码 START -->
    
      <div class="qrcode">
        <canvas id="share-qrcode"></canvas>
        <p class="notice">扫描二维码，分享此文章</p>
      </div>
    
    <!-- 二维码 END -->
    
      <!-- Utterances START -->
      <div id="utterances"></div>
      <script src="https://utteranc.es/client.js"
        repo="ltyeamin/blogtalks"
        issue-term="pathname"
        theme="github-light"
        crossorigin="anonymous"
        async></script>    
      <!-- Utterances END -->
    
  </article>
  <!-- Article END -->
  <!-- Catalog START -->
  
    <aside class="catalog-container">
  <div class="toc-main">
    <strong class="toc-title">Catalog</strong>
    
      <ol class="toc-nav"><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-nav-text">正则表达式</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#grep%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">grep命令</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%B8%B8%E7%94%A8%E9%80%89%E9%A1%B9"><span class="toc-nav-text">常用选项</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%AE%9E%E6%88%98%E7%A4%BA%E4%BE%8B"><span class="toc-nav-text">实战示例</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#sed%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">sed命令</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%91%BD%E4%BB%A4%E6%A0%BC%E5%BC%8F"><span class="toc-nav-text">命令格式</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%8F%96%E8%A1%8C%E9%80%89%E9%A1%B9"><span class="toc-nav-text">取行选项</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E6%9B%BF%E6%8D%A2%E9%80%89%E9%A1%B9"><span class="toc-nav-text">替换选项</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%AE%9E%E6%88%98%E7%A4%BA%E4%BE%8B-1"><span class="toc-nav-text">实战示例</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#awk%E5%91%BD%E4%BB%A4"><span class="toc-nav-text">awk命令</span></a><ol class="toc-nav-child"><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%91%BD%E4%BB%A4%E6%A0%BC%E5%BC%8F-1"><span class="toc-nav-text">命令格式</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E9%80%89%E9%A1%B9%E5%8F%82%E6%95%B0"><span class="toc-nav-text">选项参数</span></a></li><li class="toc-nav-item toc-nav-level-3"><a class="toc-nav-link" href="#%E5%AE%9E%E6%88%98%E7%A4%BA%E4%BE%8B-2"><span class="toc-nav-text">实战示例</span></a></li></ol></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#%E4%BC%81%E4%B8%9A%E5%AE%9E%E6%88%98-%E7%94%A8%E4%B8%89%E5%89%91%E5%AE%A2%E5%8F%96IP"><span class="toc-nav-text">企业实战-用三剑客取IP</span></a></li><li class="toc-nav-item toc-nav-level-2"><a class="toc-nav-link" href="#%E7%9B%B8%E5%85%B3%E6%96%87%E6%A1%A3"><span class="toc-nav-text">相关文档</span></a></li></ol>
    
  </div>
</aside>
  
  <!-- Catalog END -->
</main>

<script>
  (function () {
    var url = 'http://example.com/2018/05/22/Linux基础-正则表达式之三剑客/';
    var banner = ''
    if (banner !== '' && banner !== 'undefined' && banner !== 'null') {
      $('#article-banner').css({
        'background-image': 'url(' + banner + ')'
      })
    } else {
      $('#article-banner').geopattern(url)
    }
    $('.header').removeClass('fixed-header')

    // error image
    $(".markdown-content img").on('error', function() {
      $(this).attr('src', '/css/images/error_icon.png')
      $(this).css({
        'cursor': 'default'
      })
    })

    // zoom image
    $(".markdown-content img").on('click', function() {
      var src = $(this).attr('src')
      if (src !== '/css/images/error_icon.png') {
        var imageW = $(this).width()
        var imageH = $(this).height()

        var zoom = ($(window).width() * 0.95 / imageW).toFixed(2)
        zoom = zoom < 1 ? 1 : zoom
        zoom = zoom > 2 ? 2 : zoom
        var transY = (($(window).height() - imageH) / 2).toFixed(2)

        $('body').append('<div class="image-view-wrap"><div class="image-view-inner"><img src="'+ src +'" /></div></div>')
        $('.image-view-wrap').addClass('wrap-active')
        $('.image-view-wrap img').css({
          'width': `${imageW}`,
          'transform': `translate3d(0, ${transY}px, 0) scale3d(${zoom}, ${zoom}, 1)`
        })
        $('html').css('overflow', 'hidden')

        $('.image-view-wrap').on('click', function() {
          $(this).remove()
          $('html').attr('style', '')
        })
      }
    })
  })();
</script>


  <script>
    var qr = new QRious({
      element: document.getElementById('share-qrcode'),
      value: document.location.href
    });
  </script>






    <div class="scroll-top">
  <span class="arrow-icon"></span>
</div>
    <footer class="app-footer">
  <p class="copyright">
    &copy; 2024 | Proudly powered by <a href="https://hexo.io" target="_blank">Hexo</a>
    <br>
    Theme by <a target="_blank" rel="noopener" href="https://github.com/ltyeamin">tong.li</a>
  </p>
</footer>

<script>
  function async(u, c) {
    var d = document, t = 'script',
      o = d.createElement(t),
      s = d.getElementsByTagName(t)[0];
    o.src = u;
    if (c) { o.addEventListener('load', function (e) { c(null, e); }, false); }
    s.parentNode.insertBefore(o, s);
  }
</script>
<script>
  async("https://cdn.staticfile.org/fastclick/1.0.6/fastclick.min.js", function(){
    FastClick.attach(document.body);
  })
</script>

<script>
  var hasLine = 'true';
  async("https://cdn.staticfile.org/highlight.js/9.12.0/highlight.min.js", function(){
    $('figure pre').each(function(i, block) {
      var figure = $(this).parents('figure');
      if (hasLine === 'false') {
        figure.find('.gutter').hide();
      }
      hljs.configure({useBR: true});
      var lang = figure.attr('class').split(' ')[1] || 'code';
      var codeHtml = $(this).html();
      var codeTag = document.createElement('code');
      codeTag.className = lang;
      codeTag.innerHTML = codeHtml;
      $(this).attr('class', '').empty().html(codeTag);
      figure.attr('data-lang', lang.toUpperCase());
      hljs.highlightBlock(block);
    });
  })
</script>
<!-- Baidu Tongji -->



<script src='https://cdn.staticfile.org/mermaid/8.11.2/mermaid.min.js'></script>



<script src="/js/script.js"></script>


  </body>
</html>